<?Lassoscript
// Last modified 10/27/10 by Eric Landmann

// FUNCTIONALITY
// This establishes the queries and results pages for the admin Topics area
// Include for Topic search results
// The controlling record is Section_ID from the users table, which is passed as the action_param "TID"

// CHANGE NOTES
// 4/17/09
// Version 1.0
// 11/30/09
// Worked on admin area
// 10/27/10
// Fixed queries

If(!(Var_Defined('vSQLQueryTopic'))); Session_AddVariable(-Name='forums_admin', 'vSQLQueryTopic'); /If;

// Setting Variables for Nav Control
Var('vNew' = (Action_Param('New')),
	'Records_Skipped' = (Action_Param('skip')),
	'This_Group' = (Action_Param('group')),
	'Records_Per_Page' = 10,
	'SkipCount' = integer,
	'Records_Found' = integer,
	'Group_Size' = (Math_Mult:(Var('Records_Per_Page')),5));


Var('vSID' = (Action_Param('SID')));
Var('svSID' = (Action_Param('SID')));
Var('vTopic_Title' = (Action_Param('Topic_Title')));
Var('vAllow' = (Action_Param('Allow')));

// Debugging
// Var('svDebug' = 'Y');

// Output debugging filename
If($svDebug == 'Y');
	'<br>\n';
	'<strong>topic_searchresults.inc</strong><br>\n';
/If;

// If a new query, initialize all the variables
If($vNew == 'Y');
	Var('vSQL_Topic' = string);
	Var('vSQLQueryTopic' = string);
	Var('svSearchParams' = string);
	Var('svSearchParamsURL' = string);

	// Include the code for building the search params phrase
	Include(($svLibsPath)+'searchparamstopics_build.inc');

	Var('vSQLQueryTopic' = ('/* 56 Admin - Select Records by Topic */
SELECT S.Section_Title, T.Topic_ID, T.Topic_Title, T.Poster, T.Allow, T.UpdateDate, T.UpdateTime
FROM '+($svTopicsTable)+' AS T
INNER JOIN '+($svSectionsTable)+' AS S USING (Section_ID)\n'));

	// SEARCHES EVERYTHING
	If(($vSID == '') && ($vAllow == '') && ($vTopic_Title ==''));

		$vSQLQueryTopic += ' WHERE 1 ';
		$vSQLQueryTopic += 'ORDER BY T.UpdateDate DESC';

	// Some search param was passed
	Else;

		$vSQLQueryTopic += ' WHERE (';

		If($vAllow != '');
			$vSQLQueryTopic += ('(T.Allow = "'+($vAllow)+'")');
		/If;
		If((($vSID != '') || ($vTopic_Title !='')) && ($vAllow != ''));
			$vSQLQueryTopic += ' AND ';
		/If;
		If($vSID != '');
			$vSQLQueryTopic += ('(T.Section_ID = "'+($vSID)+'")');
		/If;
		If($vTopic_Title !='');
			$vSQLQueryTopic += ' AND ';
			$vSQLQueryTopic += ('(T.Topic_Title LIKE "%'+($vTopic_TItle)+'%")');
	/If;

		$vSQLQueryTopic += ')\n';
		$vSQLQueryTopic += 'ORDER BY T.UpdateDate DESC';
	
	Debug;
				('53: vSID = '+(Var('vSID'))+'<br>\n');
				('53: vTID = '+(Var('vTID'))+'<br>\n');
				('53: vTopic_Title = '+(Var('vTopic_Title'))+'<br>\n');
				('53: vSQLQueryTopic = '+(Var('vSQLQueryTopic'))+'<br>\n');
				('53: svSearchParams = '+(Var('svSearchParams'))+'<br>\n');
				('53: svSearchParamsURL = '+(Var('svSearchParamsURL'))+'<br>\n');
	/Debug;

/If;
/If;

Inline($IV_SearchSections,
	-SkipRecords=(Var('Records_Skipped')),
	-MaxRecords=(Var('Records_Per_Page')),
	-SQL=$vSQLQueryTopic);

	// Build the Nav Control Links
	Include(($svLibsPath)+'nav_control_admin.inc');

	Debug;
		('67: Error = '+(Error_CurrentError)+'<br>\n');
		('67: Found_Count = '+(Found_Count)+'<br>\n');
	/Debug;

	// If no records found, immediate redirect with error to setup_edit.lasso
	If((Found_Count) == 0);
		Var('vError'='2000');
		Var('URL' = ('setup_edit.lasso?Error='$vError'&'(Client_POSTArgs)));
		If($svDebug == 'Y');
			Debug;
				('76: URL = '+($URL)+'<br>\n');
				('<a href="'+($URL)+'">'+($URL)+'</a><br>\n');
			/Debug;
		Else;
			Redirect_URL($URL);
		/If;
	/If;

	// If only one record, redirect immediately to setup_editrecord.lasso
	// This shortcut skips past the search results page if there is only one record found
	// Example URL:
	// http://127.0.0.1/admin/setup_editrecord.lasso?DataType=Topic&TID=2&New=Y
	If((Found_Count) == 1);
		Var('URL' = ('setup_editrecord.lasso?DataType=Topic&TID='+(Field('Topic_ID'))+'&New=Y'));
		If($svDebug == 'Y');
			Debug;
				('96: URL = '+($URL)+'<br>\n');
				('<a href="'+($URL)+'">'+($URL)+'</a><br>\n');
			/Debug;
		Else;
			Redirect_URL($URL);
		/If;
	/If;
?>
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" bgcolor="black">
	<tr>
		<td width="620" valign="top" bgcolor="#FFFFFF">
			<div align="left">
				<h2>Topic Search Results</h2>
				<p><font color="red">[Var:'svSearchParams', -EncodeNone]</font>&nbsp;&nbsp;&nbsp;<a href="[$svSiteFolderRoot]/admin/setup_edit.lasso[$svSearchParamsURL]">Search Again</a></p>
			</div>
			<div align="left">
				<table width="535" cellspacing="0" cellpadding="5">
					<tr bgcolor="#000000">
						<td valign="middle" width="140" class="tablehead_10_white">Section</td>
						<td valign="middle" class="tablehead_10_white">Topic Name</td>
						<td valign="middle" width="120" class="tablehead_10_white">Updated</td>
						<td valign="middle" width="80" class="tablehead_10_white">Poster</td>
						<td valign="middle" width="30" class="tablehead_10_white">Allow</td>
					</tr>
<?LassoScript
		Records;
			Var(
				'vSection_Title' = (Field('Section_Title')),
				'vTID' = (Field('Topic_ID')),
				'vSID' = (Field('Section_ID')),
				'vTopic_Title' = (Field('Topic_Title')),
				'vPoster' = (Field('Poster')),
				'vAllow'=(Field('Allow')),
				'vUpdateDate'=(Field('UpdateDate')),
				'vUpdateTime'=(Field('UpdateTime'))
				);
?>
				<tr <?Lassoscript If((Loop_Count) %2 == 0); 'bgcolor = "#F5F5F5"'; Else; 'bgcolor = "#FFFFFF"'; /If; ?> >
					<td valign="middle" class="tabletext_10_black">
						<strong>[Var('vSection_Title')]</strong></td>
					<td valign="middle" class="tabletext_10_black">
						<strong><a href="setup_editrecord.lasso?DataType=Topic&TID=[Var('vTID')]&New=Y">[Var('vTopic_Title')]</a></strong></td>
					<td valign="middle" class="tabletext_10_black">
						[Var('vUpdateDate')] [Var('vUpdateTime')]
					</td>
					<td valign="middle" class="tabletext_10_black">
						[Var('vPoster')]
					</td>
					<td valign="middle" class="tabletext_10_black">
						[Var('vAllow')]
					</td>
				</tr>
		[/Records]
	<tr>
		<td colspan="4" valign="top" bgcolor="#cccccc">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="4" valign="middle" align="left" bgcolor="#f5f5f5">
			<div align="center">[$NavControlLinks]</div>
		</td>
	</tr>
		</table>
[/Inline]
